<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
require_once('../inclure.php');
 $message = ''; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];

if(isset($_POST['Code_element'])){
	//$existe = existeChampAutre($database_microassur, $microassur, 'elementdico', 'Code_element', GetSQLValueString($_POST['Code_element'], "text"));
	$existe = existeDeuxChamp($database_microassur, $microassur, 'elementdico', 'Code_element', 'Identifiant_table', @$_POST['Code_element'], @$_POST['Identifiant_table']);
	//	var_dump($existe);
}
if(isset($_POST['Code_element']) && empty($_POST['Code_element'])){
	$message = colorer("Vous devez saisir le CODE.","false");
}elseif(isset($_POST['Code_element']) && !veriflibelle1('Code_element') && caracteres_interdits($_POST['Code_element'])==1){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits.","false");
}elseif(isset($_POST['Nom_element']) && empty($_POST['Nom_element'])){
	$message = colorer("Vous devez saisir le nom.","false");
}elseif(isset($_POST['Nom_element']) && caracteres_interdits($_POST['Nom_element'])==1){
	$message = colorer("Le nom saisi comporte des caractères spéciaux interdits.","false");
}elseif(isset($_POST['Identifiant_table']) && empty($_POST['Identifiant_table'])){
	$message = colorer("", "false"); //("Cet élément dico n\'est pas rattaché à un dictionnaire,vous devez saisir un dictionnaire","false");
}elseif(isset($existe) && $existe!=0){
	$message = colorer("Cet élément dictionnaire existe déjà","false");
}// Fin if($existe=='1')
else { // Else if($existe=='1')
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	  $insertSQL = sprintf("INSERT INTO elementdico (Identifiant_element, Code_element, Nom_element, Identifiant_table) VALUES (%s, %s, %s, %s)",
						   GetSQLValueString($_POST['Identifiant_element'], "int"), 
						   GetSQLValueString($_POST['Code_element'], "text"),
						   GetSQLValueString($_POST['Nom_element'], "text"),
						   GetSQLValueString($_POST['Identifiant_table'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	   mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  mysql_query("commit");
		  	$message = colorer("L'élément dictionnaire est créé","true");
			unset($_POST);
		}
	  else
	  	{  mysql_query("rollback");$message = colorer("Contacter la DSI, erreur création élément dictionnaire","false");}
	}
}  // End Else if($existe=='1')

$maxRows_rs_eltdico = 10;
$pageNum_rs_eltdico = 0;
if (isset($_GET['pageNum_rs_eltdico'])) {
  $pageNum_rs_eltdico = $_GET['pageNum_rs_eltdico'];
}
$startRow_rs_eltdico = $pageNum_rs_eltdico * $maxRows_rs_eltdico;

mysql_select_db($database_microassur, $microassur);
$query_rs_eltdico = "SELECT * FROM elementdico";
//$query_limit_rs_eltdico = sprintf("%s LIMIT %d, %d", $query_rs_eltdico, $startRow_rs_eltdico, $maxRows_rs_eltdico);
$query_limit_rs_eltdico = $query_rs_eltdico;
$rs_eltdico = mysql_query($query_limit_rs_eltdico, $microassur) or die(mysql_error());
$row_rs_eltdico = mysql_fetch_assoc($rs_eltdico);

if (isset($_GET['totalRows_rs_eltdico'])) {
  $totalRows_rs_eltdico = $_GET['totalRows_rs_eltdico'];
} else {
  $all_rs_eltdico = mysql_query($query_rs_eltdico);
  $totalRows_rs_eltdico = mysql_num_rows($all_rs_eltdico);
}
$totalPages_rs_eltdico = ceil($totalRows_rs_eltdico/$maxRows_rs_eltdico)-1;

?>
<?php 
	if(isset($message) && !empty($message)){
		//echo $message;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Eléments dictionnaire";
					$nommodule2="Liste des éléments";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
                <table class="tab_list bordh">
					<tr id="niv1">
						  <td class="col1">CODE</td>
						  <td class="col2">LIBELLE</td>
						  <td class="col3">DICTIONNAIRE</td>
					</tr>
                </table>
				<div class="content_table">
					<table class="tab_list">
						  <?php $i=1; do 	{
						$col1_rs_dico = "-1";
						if (isset($row_rs_eltdico['Identifiant_table'])) {
						  $col1_rs_dico = $row_rs_eltdico['Identifiant_table'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_dico = sprintf("SELECT * FROM dictionnaire WHERE Identifiant_table = %s", GetSQLValueString($col1_rs_dico, "text"));
						$rs_dico = mysql_query($query_rs_dico, $microassur) or die(mysql_error());
						$row_rs_dico = mysql_fetch_assoc($rs_dico);
						$totalRows_rs_dico = mysql_num_rows($rs_dico);

						mysql_select_db($database_microassur, $microassur);
						$query_rs_dico_all = "SELECT Identifiant_table, Libelle_table FROM dictionnaire";
						$rs_dico_all = mysql_query($query_rs_dico_all, $microassur) or die(mysql_error());
						$row_rs_dico_all = mysql_fetch_assoc($rs_dico_all);
						$totalRows_rs_dico_all = mysql_num_rows($rs_dico_all);

						$queryString_rs_eltdico = "";
						if (!empty($_SERVER['QUERY_STRING'])) {
						  $params = explode("&", $_SERVER['QUERY_STRING']);
						  $newParams = array();
						  foreach ($params as $param) {
							if (stristr($param, "pageNum_rs_eltdico") == false && 
								stristr($param, "totalRows_rs_eltdico") == false) {
							  array_push($newParams, $param);
							}
						  }
						  if (count($newParams) != 0) {
							$queryString_rs_eltdico = "&" . htmlentities(implode("&", $newParams));
						  }
						}
						$queryString_rs_eltdico = sprintf("&totalRows_rs_eltdico=%d%s", $totalRows_rs_eltdico, $queryString_rs_eltdico);
						  
						   ?>
						  <?php if ($totalRows_rs_eltdico > 0) { // Show if recordset not empty ?>
							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="col1"><a href="modelementdico.php?Identifiant_element=<?php echo $row_rs_eltdico['Identifiant_element']; ?>"><?php echo $row_rs_eltdico['Code_element']; ?></a></td>
							  <td class="col2"><a href="modelementdico.php?Identifiant_element=<?php echo $row_rs_eltdico['Identifiant_element']; ?>"><?php echo $row_rs_eltdico['Nom_element']; ?></a></td>
							  <td class="col3"><a href="modelementdico.php?Identifiant_element=<?php echo $row_rs_eltdico['Identifiant_element']; ?>"><?php echo $row_rs_dico['Libelle_table'];  ?></a></td>
							</tr>
							<?php } // Show if recordset not empty ?>

						  <?php $i++; } while ($row_rs_eltdico = mysql_fetch_assoc($rs_eltdico)); ?>
						</table>
				</div>
			</div>
			<div class="imgright"></div>
		<div class="cleared"></div>
		<!--/***************************************/-->
		<div id="br"></div>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
					<div class="formulaire">
							  <table class="tabform">
								<tr>
								  <td>CODE</td>
								  <td><input class="resize_1" type="text" name="Code_element"  maxlength="5" value="<?php echo remind_text('Code_element'); ?>" size="32" /></td>
								</tr>
								<tr>
								  <td>NOM </td>
								  <td><input type="text" name="Nom_element"  maxlength="30" value="<?php echo remind_text('Nom_element'); ?>" size="32" /></td>
								</tr>
								<tr>
								  <td>DICTIONNAIRE</td>
								  <td><select name="Identifiant_table">
									<?php 
							do {  
							?>
									<option value="<?php echo $row_rs_dico_all['Identifiant_table']?>" <?php echo remind_select('Identifiant_table', $row_rs_dico_all['Identifiant_table']) ?> ><?php echo $row_rs_dico_all['Libelle_table']?></option>
									<?php
							} while ($row_rs_dico_all = mysql_fetch_assoc($rs_dico_all));
							?>
								  </select>
								  </td>
								</tr>
							  </table>
					</div>
			</div>
			<div class="imgright"></div>				
				<div class="cleared"></div>
				
					<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
					<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
					
				<div class="cleared"></div>
  <input type="hidden" name="Identifiant_element" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs_eltdico);
mysql_free_result($rs_dico);
mysql_free_result($rs_dico_all);
?>
